package com.rongshu.sendmessage.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.rongshu.sendmessage.R;

import java.util.List;

/**
 * 往期开奖数据适配器
 */
public class ImageDataAdapter extends RecyclerView.Adapter<ImageDataAdapter.ViewHolder> {

    private List<String> dataArr;
    private OnItemClickListener mOnItemClickListener;

    public ImageDataAdapter(List<String> dataArr) {
        this.dataArr = dataArr;
    }

    public void setList(List<String> list) {
        this.dataArr = list;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup recyclerView, int viewType) {
        Context context = recyclerView.getContext();
        View view;
//        if (whidth==2){
             view = LayoutInflater.from(context).inflate(R.layout.menu_grid_item_2, recyclerView, false);
//        }else{
//            view = LayoutInflater.from(context).inflate(R.layout.menu_grid_item_3, recyclerView, false);
//        }

        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        View itemView = holder.itemView;

        ImageView iv_image = itemView.findViewById(R.id.iv_image);
        String pic = dataArr.get(position);
//        Bitmap bitmap = ImageLoader.getInstance().loadImageSync(pic);
//        int height = bitmap.getHeight();
//        if (dataArr.size()==1){
//            layoutParams.height=300;
//        }else {
//            layoutParams.height=220;
//        }

        ImageLoader.getInstance().displayImage(pic,iv_image);
//        item_tv.setImageURI(pic);
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(v -> mOnItemClickListener.onClick(position));
            holder.itemView.setOnLongClickListener(v -> {
                mOnItemClickListener.onLongClick(position);
                return false;
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataArr.size();
    }

    public interface OnItemClickListener {
        void onClick(int position);

        void onLongClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
